﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Jxym.DBUtility;
using Jxym.wlkc2011.Module.Default.model;
using Newtonsoft.Json;
using System.Data.SqlClient;

namespace Jxym.wlkc2011.Module.Default.server.Files
{
    public partial class FoldersTree : System.Web.UI.Page
    {
        string str = "", children = ",\"children\":";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetFoldersTree(0);

                Response.Write(str.TrimStart(children.ToCharArray()));
            }
        }
        MsSqlDal<Folders> iD = new MsSqlDal<Folders>();

        private string IsLaef(int id)
        {
            IList<Folders> list = iD.GetList("ParentID=" + id, null, "", 0);
            if (list.Count > 0)
                return "false";
            return "true";
        }

        private void GetFoldersTree(int p)
        {
            string sw = "ParentID=" + p;

            if (p == 0)
                sw = "ParentID=" + p;

            IList<Folders> iDlist = iD.GetList(sw, null, "", 0);

            if (iDlist.Count > 0)
            {
                str += children + "[";

                foreach (Folders d in iDlist)
                {
                    str += "{'id':'" + d.ID + "','text':'" + d.Name + "','leaf':" + IsLaef(d.ID);

                    GetFoldersTree(d.ID);

                    str += "},";
                }

                str = str.TrimEnd(',');

                str += "]";
            }
        }
    }
}